package cn.com.tctec.faked;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 生成一段从 startTime 到 endTime 模拟运行状态数据基础对象
 *
 * @author jiangz
 * @version 1.0 Created on 2019年9月6日 下午4:33:30
 */
public class RunStatusItem {

    private SimpleDateFormat stdFormat = new SimpleDateFormat(Constant.DATETIME_FMT);

    private String tagF;
    private Date startTime;
    private Date endTime;

    /**
     * 模拟参数
     * <p>
     * storeCycle 存储周期（秒）
     * durationFixed 固定持续时长（秒）
     * durationVar 浮动持续时长（秒）
     */
    private Long storeCycle;
    private Long durationFixed;
    private Long durationVar;

    public void print() {
        Long start = this.startTime.getTime() / 1000;
        Long end = this.endTime.getTime() / 1000;
        while (start < end) {
            Long currentTimeMillis = System.currentTimeMillis();
            Long millis = currentTimeMillis % 1000;

            // status 持续 duration 秒
            Long status = millis % 5;
            Long duration = this.durationFixed + this.durationVar * (millis % 3 - 1);

            for (int i = 0; this.storeCycle * i < duration; i++) {
                if (start < end) {
                    RunStatusDatum datum = new RunStatusDatum(this.tagF,
                            status.intValue(), start);
                    datum.geneInfluxInsertString();
                    start += storeCycle;
                } else {
                    break;
                }
            }
        }
    }

    public RunStatusItem(String tagF, String startTimeStdStr,
                         String endTimeStdStr, Long storeCycle, Long durationFixed, Long durationVar)
            throws ParseException {
        super();
        this.tagF = tagF;
        this.startTime = stdFormat.parse(startTimeStdStr);
        this.endTime = stdFormat.parse(endTimeStdStr);
        this.storeCycle = storeCycle;
        this.durationFixed = durationFixed;
        this.durationVar = durationVar;
    }

    public String getTagF() {
        return tagF;
    }

    public void setTagF(String tagF) {
        this.tagF = tagF;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Long getStoreCycle() {
        return storeCycle;
    }

    public void setStoreCycle(Long storeCycle) {
        this.storeCycle = storeCycle;
    }

    public Long getDurationFixed() {
        return durationFixed;
    }

    public void setDurationFixed(Long durationFixed) {
        this.durationFixed = durationFixed;
    }

    public Long getDurationVar() {
        return durationVar;
    }

    public void setDurationVar(Long durationVar) {
        this.durationVar = durationVar;
    }
}
